package com.uft.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.uft.model.dao.ProfileDAO;
import com.uft.model.entity.DPProfile;

@Controller
public class DashboardController {
	private  static Logger  logger = Logger.getLogger(DatapowerController.class);
	
	@RequestMapping(value="/getDashboardView", method = RequestMethod.GET)
	public ModelAndView getDashboardView(HttpServletRequest request) {
		ModelAndView model = new ModelAndView("dashboard");
		return model;
	}
	
	@RequestMapping(value="/getJVMRuntimeStats", method = RequestMethod.POST)
	@ResponseBody
	public String getJVMRuntimeStats(HttpServletRequest request){
		
		String result="";
		int mb = 1024*1024;
		HashMap<String,Object> jvmStats= new HashMap<String,Object>();
		logger.debug("##### Heap utilization statistics [MB] #####");
		ObjectMapper mapper = new ObjectMapper();
		try {
			Runtime runtime = Runtime.getRuntime();
			long usedMemory=(runtime.totalMemory() - runtime.freeMemory()) / mb;
			long freeMemory=runtime.freeMemory() / mb;
			long totalMemory=runtime.totalMemory() / mb;
			long maxMemory=runtime.maxMemory() / mb;
			
			jvmStats.put("genTimeStamp",Calendar.getInstance().getTimeInMillis() );
			jvmStats.put("usedMemory", usedMemory);
			jvmStats.put("freeMemory", freeMemory);
			jvmStats.put("totalMemory", totalMemory);
			jvmStats.put("maxMemory", maxMemory);
			result=mapper.writeValueAsString(jvmStats);
			logger.debug("Memory Stats: "+result);
			
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	
}
